//
//  Challenge.m
//  Tables
//
//  Created by mathias Muller on 8/23/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "Challenge.h"
#import "Operation.h"

@implementation Challenge

@synthesize operations;
@synthesize currentOperation;
@synthesize conf, startDate, timeTaken;

+ (Challenge *) startNew:(ChallengeConfiguration*)conf{
	Challenge * instance = [[Challenge  alloc] init];
	instance.conf = conf;
	instance.operations = [conf getOperations];
	instance.currentOperation = [instance.operations objectAtIndex:0];
	return [instance autorelease]; //autorelease since static constructor has to
}

- (void) startTimer {
	self.startDate = [NSDate date];
}

- (NSTimeInterval) stopTimer {
	self.timeTaken = [[NSDate date] timeIntervalSinceDate:self.startDate];
	return self.timeTaken;
}

- (Operation *)nextOperation {
	NSInteger currIndex = [self.operations indexOfObject:self.currentOperation];
	if(currIndex < [self.operations count]-1) {
		return self.currentOperation = [self.operations objectAtIndex:currIndex+1];
	}
	return NULL;
}

- (BOOL)hasNextOperation {
	return !([self.currentOperation isEqual:[self.operations lastObject]]);
}

- (BOOL)isCorrect {
	for (int i=0; i< [self.operations count]; i++) {
		Operation * o = [self.operations objectAtIndex:i]; 
		if(![o isCorrect]) {
			return FALSE;
		}
	}
	return TRUE;
}

- (NSInteger)getMaxResponseValue {
	NSInteger max = -1;
	for (int i=0; i< [self.operations count]; i++) {
		Operation * o = [self.operations objectAtIndex:i]; 
		if([o getResult]> max) {
			max = [o getResult];
		}
	}
	return max;
}
@end
